define(function(require,exports,module){
	
	require('common');
	//检测类
	var Auth = require('qd-auth-1.0');

	var changeStatus = 0;//0代表添加 1代表修改
	
	
	function layerTip(type,msg){
		
		layer.closeAll();
		var html ="";
		if(type==1){
			html = '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont okIco">&#xe63d;</i><span>'+msg+'</span></div><div class="tipsMsg">3秒后窗口自动关闭</div></div>';		    
		}else if(type==2){
			html = '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+msg+'</span></div><div class="tipsMsg">3秒后窗口自动关闭</div></div>';		    			
		}
				
		window.layer.open({
		    type: 1,
		    closeBtn: 1,
		    title: "&nbsp;",
		    area: ['384px', '186px'],
		    border: [1, 1, '#cecfd0'],
		    time:3000,
		    content:html,
		    cancel: function(index){
		    	window.location.reload();
		    },
		    end: function(){
		    	window.location.reload();
		    },
		    success: function(layero){
		    	var time =3; 
		    	function closeTime () {
					time--;
					$(".tipsMsg").html(time+"秒后窗口自动关闭");
				}
		    	setInterval(function() {
					closeTime();
				}, 1000);
		    }
			    
		});
		
		
	}

	//-S 添加/修改地址
	$(".addressForm").validate({
		rules:{
			name:{
				required:true,
				rangelength:[2,25],
				chnName:true
			},
			mobile:{
				 required:function(){
				 	if($('.areaCode').val() == '')
				 	{
				 		return true;
				 	}
				 	else
				 	{
				 		return false;
				 	}
				 },
				 isMobile:true
			},
			phone:{
				 required:function(){
				 	if($('.phoneNumber').val() == '')
				 	{
				 		return true;
				 	}
				 	else
				 	{
				 		return false;
				 	}
				 },
				 fixNumber:true
			},
			postalCode:{
				isZipCode:true
			},
			address:{
				required:true,
				rangelength:[5,120]
			}
		},
		messages:{
			name:{
				required:"请输入您的姓名",
				rangelength:"收货人姓名应为2-25个字符，一个汉字为一个字符",
				chnName:"真实姓名仅为中文"
			},
			mobile:{
				required:"请输入手机号码",
				isMobile:"请正确输入您的手机号码"
			},
			phone:{
				required:"请输入电话号码",
				fixNumber:"请正确输入您的电话号码（以0开头）"
			},
			postalCode:{
				isZipCode:"请正确输入邮政编码"
			},
			address: {
				required: "请输入详细地址",
				rangelength:"5-120个字符，一个汉字为一个字符"
			  }
		},
		errorElement:'em',
		errorPlacement:function(error,element){
			element.parents("li").find("span").html(error);
	    },
	    success:function(label){
	    },  
	    submitHandler:function(form){
	    	var url = "";
	    	//判断是否可以保存
	    	if(parseInt($('.remainCount').text()) > 0 || changeStatus == 1)//值为0或者changeStatus为1
	    	{
	    		//0代表添加 1代表修改
	    		if(changeStatus == 0) {
	    			url = '/member/score/receivingInfo/doAppend.html';
	    		} else if(changeStatus == 1) {
	    			url = '/member/score/receivingInfo/doModify.html';
	    		}
				$(".addressForm").ajaxSubmit({
					dataType:'json',
					url:url,
					success:function(data){
						//单点登录检测
						if(Auth.checkLogin(arguments[0]) == false) {return ;}

						if(data.result){
							layerTip(1,"保存成功"); 
    					}else{
    						layerTip(2,"最多保存5条地址！"); 
    					}
					}
				});
	    	}
	    	else
	    	{
	    		layerTip(2,"最多保存5条地址！");
	    	}
			
        } 
	});

	//修改地址
	$('.changeAdd').on('click',function(){
		changeStatus = 1;
		var id = $(this).parent().attr('data-id');
		//取值	
		$.ajax({
			url: '/member/score/receivingInfo/detail.html?id=' + id + '&randomTime=' + (new Date()).getTime(),
			type: 'get',
			dataType: 'json',
			success:function(data)
			{
				//单点登录检测
				Auth.checkLogin(arguments[0]);

				var isview = $("#addressMaindiv").is(":hidden");
				if(isview){ //如果div隐藏，则在修改的时候，div显示出来
					$("#addressMaindiv").show();
				}
				//取值
				var dataV = data.data,
					status = dataV.status,
					name = dataV.name,
					mobile = dataV.mobile,
					phone = dataV.phone,
					postalCode = dataV.postalCode,
					province = dataV.province,
					city = dataV.city,
					area = dataV.area,
					address = dataV.address,
					defultInfo = dataV.defultInfo;
				//赋值
				$('input[name="name"]').val(name);
				$('input[name="address"]').val(address);
				$('input[name="postalCode"]').val(postalCode);
				$('input[name="mobile"]').val(mobile);
				$('input[name="phone"]').val(phone);
				$('input[name="id"]').val(id);
				$('input[name="status"]').val(status);
				$('.addressInput4').text(address);
				if(defultInfo == true) {
					$('input[name="defultInfo"]').prop('checked',true);
				} else if(defultInfo == false) {
					$('input[name="defultInfo"]').prop('checked',false);
				}
				require.async('jquery-citySelect/jquery.cityselect',function(){
					$("#city").citySelect({
						url:"../../../themes/theme_default/media/js/jquery-citySelect/city.json",
						prov:province, //省份 
						city:city, //城市 
						dist:area, //区县 
					    required:true,
					    nodata:"none" //当子集无数据时，隐藏select 
					});
				})
				//判断是修改还是新增
				if(changeStatus == 0) {
					$('.scoreTitle').html('新增收货信息');
				} else if(changeStatus == 1) {
					$('.scoreTitle').html('修改收货信息');
				}
			}
		})
	})
	
	//设为默认地址 快捷键
	$('.hoverDefault').each(function(){
		var oTr = $(this).find('.defaultAddress'),
			oDe = $(this).find('.defultInfo0').attr('data-val');
		if(oDe == 1) {
			oTr.text('默认地址');
			oTr.css("color","#2675b9");
			oTr.show();
		} else if(oDe == 0) {
			oTr.text('设为默认');
		}
	})

	$('.defaultAddress').click(function(){
		var dataVal = $(this).parents().find('.defultInfo0').attr('data-val'),
			id = $(this).parents().find('.defultInfo0').attr('data-id');
			if(dataVal == 0)
			{
				$.ajax({
					url: '/member/score/receivingInfo/setDefult.html?id=' + id + '&randomTime=' + (new Date()).getTime(),
					type: 'get',
					dataType: 'json',
					success:function() {
						window.location.reload();
					}
				})	
			}			
	})


	//点击修改地址 置顶
	require.async("commonJS/rollTo",function(){
		$(".changeAdd").rollTo({
			oFinish: ".user_right ", //要滚动到的元素
			sSpeed: "300",  //滚动速度
			bMonitor: false, //是否楼层监听
			sClass: "", //楼层监听时需要添加的样式
			iBias:-18,
			fnAdditional: "" //追加方法
		});
	})	
	
	//删除
	$('.disableRecord').on('click',function(){
		var $this = $(this),
			parents = $this.parents('tr'),
		    id = $this.parent().attr('data-id');
			//构造确认框DOM
			window.layer.open({
			    type: 1,
			    closeBtn: 1,
                title: "&nbsp;",
			    area: ['450px', '190px'],
			    border: [1, 1, '#cecfd0'],
			    content: '<div class="tipsWrap w450"><div class="tipsTxt"><i class="iconfont tipIco">&#xe63c;</i><span>确定删除地址？</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn">确认</a><a href="javascript:;" class="cancleBtn">取消</a></div></div>'
			    
			});	
			//确认操作
			$(".okBtn").click(function(){
				$.ajax({
					url:"/member/score/receivingInfo/disable.html?id=" + id + "&randomTime=" + (new Date()).getTime(),
					type:"post",
					success: function(data){
						//单点登录检测
						if(Auth.checkLogin(arguments[0]) == false) {return ;}

						if(data.result){
							layer.closeAll();
							layerTip(1,"收货信息删除成功！");
						}
						else
						{
							layer.closeAll();
							window.layer.open({
							    type: 1,
							    closeBtn: 1,
				                title: "&nbsp;",
							    area: ['384px', '186px'],
							    border: [1, 1, '#cecfd0'],
							    time:3000,
							    content: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>删除失败！</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn failBtn">确定</a></div></div>',
							    close: function(index){
							    	layer.closeAll();
							    }
							});
							$(".failBtn").click(function(){
								layer.closeAll();
							});
						}						
					}			
				});				
			});
			//删除操作
			$(".cancleBtn").click(function(){
				layer.closeAll();
			});

	});

	//如果保存的地址为0，就隐藏表格
 	if($('.hasAddress').text() == 0) {
 		$('.addressList').hide();
 	} else {
 		$('.addressList').show();
 	}


	//加载城市选择插件
	require.async('jquery-citySelect/jquery.cityselect', function(){
		$("#city").citySelect({
			url:"../../../themes/theme_default/media/js/jquery-citySelect/city.json",
			prov:"浙江", //省份 
			city:"杭州", //城市 
			dist:"西湖区", //区县 
		    required:true,
		    nodata:"none" //当子集无数据时，隐藏select 
		});
	});

});
